<template>
    <Modal :mask-closable="false" v-model="toolMaintenanceVisible" :title="isAdd ? '新增权限' : '编辑权限'">
        <Form class="formList" ref="rootForm" :model="formItem" :label-width="100" :rules="validateMalfunction">
            <FormItem label="人员：" prop="showName">
                <Select v-model="formItem.showName" filterable clearable :placeholder="`${$t('common.selectInfo')}`"
                    :remote-method="getList" :loading="loading" class="Input-wrapper">
                    <Option v-for="(item, index) of list" :value="item.showName" :key="index">{{ item.showName }}</Option>
                </Select>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="default" @click="handleCancel">{{ $t('common.Cancle') }}</Button>
            <Button type="primary" :loading="loading" @click="handleOk">{{ $t('maintenance.Confirmed') }}</Button>
        </div>
    </Modal>
</template>

<script>
export default {
    name: "specialRootModal",
    data() {
        return {
            isAdd: false,
            toolMaintenanceVisible: false,
            loading: false,
            formItem: {
                showName: '',
            },
            validateMalfunction: {
                showName: [
                    { required: true, message: '请选择人员！', trigger: 'change' }
                ],
            },
            list: []
        }
    },
    methods: {
        getList(query) {
            let str = ''
            str = query.includes('-') ? query.substr(0, query.indexOf('-')) : query
            if (str !== '' && str) {
                this.loading = true
                this.axios({
                    method: 'get',
                    url: '/basic/user/searchUser',
                    params: {
                        key: str
                    }
                }).then(res => {
                    if (res.code === 200) {
                        this.list = res.data.map(item => {
                            return {
                                ...item,
                                showName: item.empno + '-' + item.realname
                            }
                        })
                    } else {
                        this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                    }
                }).finally(() => {
                    this.loading = false
                })
            } else {
                this.list = []
            }
        },
        show(type, record) {
            
            this.toolMaintenanceVisible = true
            if (type === 'add') {
                this.$refs.rootForm.resetFields();
                this.isAdd = true
            } else {
                this.isAdd = false
                this.formItem = {
                    ...record
                }
            }
        },
        handleOk() {
            console.log(this.list)
            let userId = this.list[0].id
            this.loading = true
            this.$refs.rootForm.validate((valid) => {
                console.log(valid)
                if (valid) {
                    if (this.isAdd) {
                        let params = {
                            userId,
                            userName: this.formItem.showName,
                        }
                        this.axios({
                            method: 'post',
                            url: '/dms/dmsDevicePic/addWorkOrderleader',
                            params: params
                        }).then(res => {

                            if (res.code === 200) {
                                this.toolMaintenanceVisible = false
                                this.loading = false
                                this.$Message.success('新增成功！')
                                this.$refs.rootForm.resetFields();
                                this.$emit('refresh')
                            } else {
                                this.toolMaintenanceVisible = false
                                this.loading = false
                                this.$Message.error(res.msg)
                            }
                        })
                    } else {
                        // let params = {
                        //     id,
                        //     userId,
                        //     userName
                        // }
                        // this.axios({
                        //     method: 'put',
                        //     url: '/workorder/workorderTool/edit',
                        //     data: params
                        // }).then(res => {
                        //     
                        //     if (res.code === 200) {
                        //         this.toolMaintenanceVisible = false
                        //         this.loading = false
                        //         this.$Message.success('编辑成功！')
                        //         this.$refs.rootForm.resetFields();
                        //         this.$emit('refresh')
                        //     }else {
                        //         this.toolMaintenanceVisible = false
                        //         this.loading = false
                        //         this.$Message.error(res.msg)
                        //     }
                        // })
                    }
                } else {
                    this.loading = false
                }
            })
        },
        handleCancel() {
            this.$refs.rootForm.resetFields();
            this.toolMaintenanceVisible = false
        }
    }
}
</script>

<style scoped></style>
